const express = require('express');
const router = express.Router();
const db = require('../../db/index');
router.get('/', (req, res) => {
  var pc_count;
  var pi_count;
  let ac_count;
  let ai_count;
  // 项目分类数
  var sql = `SELECT count(*) AS pc_count FROM product_classification`;
  db.query(sql, (err, results) => {
    if (err) return res.send({ code: 0, msg: err.message });
    // console.log(results[0].pc_count);
    pc_count = results[0].pc_count;
  });
  // 项目数量
  var sql2 = `SELECT count(*) AS pi_count FROM product_information_management`;
  db.query(sql2, (err, results) => {
    if (err) return res.send({ code: 0, msg: err.message });
    // console.log(results[0].pc_count);
    pi_count = results[0].pi_count;
    // console.log(pi_count);
  });
  // 文章分类数量
  var ac_countSQL = `SELECT count(*) AS count FROM article_classification`;
  db.query(ac_countSQL, (err, results) => {
    if (err) return res.send({ code: 0, msg: err.message });
    // console.log(results[0].pc_count);
    ac_count = results[0].count;
  });
  // 文章数量
  var ai_countSQL = `SELECT COUNT(*) AS count FROM article`;
  db.query(ai_countSQL, (err, results) => {
    if (err) return res.send({ code: 0, msg: err.message });
    // console.log(results[0].pc_count);
    ai_count = results[0].count;
  });
  setTimeout(() => {
    // console.log(pc_count, pi_count);
    res.render('admin/echarts', { data: { pc_count, pi_count, ac_count, ai_count } });
  }, 50);
});

router.get('/list', (req, res) => {
  var projectData;
  var articleDate;
  var artcileVisit = [];
  var articleName = [];
  //查询产品分类和产品数量
  var sql = `SELECT count(*) AS value,pc_name AS name FROM product_information_management,product_classification WHERE product_classification.pc_id=product_information_management.pc_id GROUP BY product_classification.pc_id`;
  db.query(sql, (err, results) => {
    if (err) return res.send({ code: 0, msg: err.message });
    if (results.length !== 0) {
      projectData = results;
      // console.log(results);
    }
  });
  var articleSQL = `SELECT count(*) AS value,ac_name AS name FROM article,article_classification WHERE article_classification.ac_id=article.ac_id GROUP BY article_classification.ac_id`;
  db.query(articleSQL, (err, results) => {
    if (err) return res.send({ code: 0, msg: err.message });
    if (results.length !== 0) {
      articleDate = results;
      // console.log(results);
    }
  });
  // 文章访问量
  var artcileVisitSQL = `SELECT  article_visit AS visit FROM article`;
  db.query(artcileVisitSQL, (err, results) => {
    if (err) return res.send({ code: 0, msg: err.message });
    if (results.length !== 0) {
      // artcileVisit = results;
      for (const item of results) {
        // console.log(item.visit);
        artcileVisit.push(item.visit);
      }
      // console.log(artcileVisit); //,article_visit
    }
  });
  // 文章访问量的名字
  var articleNameSQL = `SELECT article_name as name FROM article`;
  db.query(articleNameSQL, (err, results) => {
    if (err) return res.send({ code: 0, msg: err.message });
    if (results.length !== 0) {
      // articleName = results;
      // console.log(results); //,article_name
      for (const item of results) {
        // console.log(item.visit);
        articleName.push(item.name);
      }
      // console.log(articleName); //,article_visit
    }
  });
  setTimeout(() => {
    // console.log(articleDate);
    res.send({ code: 1, msg: '查询成功', data: { projectData, articleDate,artcileVisit,articleName } });
  }, 500);
});
module.exports = router;
